﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using Microsoft.Phone.Data.Linq;  
using System.Data.Linq;  
using System.Data.Linq.Mapping;  
using System.ComponentModel;

namespace Loctime
{
    public partial class App : Application
    {
        /// <summary>
        /// Provides easy access to the root frame of the Phone Application.
        /// </summary>
        /// <returns>The root frame of the Phone Application.</returns>
        public PhoneApplicationFrame RootFrame { get; private set; }

        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Global handler for uncaught exceptions. 
            // Note that exceptions thrown by ApplicationBarItem.Click will not get caught here.
            UnhandledException += Application_UnhandledException;

            // Show graphics profiling information while debugging.
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // Display the current frame rate counters.
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                // Show the areas of the app that are being redrawn in each frame.
                //Application.Current.Host.Settings.EnableRedrawRegions = true;

                // Enable non-production analysis visualization mode, 
                // which shows areas of a page that are being GPU accelerated with a colored overlay.
                //Application.Current.Host.Settings.EnableCacheVisualization = true;
            }

            // Standard Silverlight initialization
            InitializeComponent();

            // Phone-specific initialization
            InitializePhoneApplication();

            #region 
            using (MyDataContext dc = new MyDataContext())
            {
                if (dc.DatabaseExists() == false)
                {
                    dc.CreateDatabase();
                }
            }
            #endregion  
        }




        // Code to execute when the application is launching (eg, from Start)
        // This code will not execute when the application is reactivated
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
        }

        // Code to execute when the application is activated (brought to foreground)
        // This code will not execute when the application is first launched
        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
        }

        // Code to execute when the application is deactivated (sent to background)
        // This code will not execute when the application is closing
        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
        }

        // Code to execute when the application is closing (eg, user hit Back)
        // This code will not execute when the application is deactivated
        private void Application_Closing(object sender, ClosingEventArgs e)
        {
        }

        // Code to execute if a navigation fails
        private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // A navigation has failed; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
        }

        // Code to execute on Unhandled Exceptions
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred; break into the debugger
                System.Diagnostics.Debugger.Break();
            }
        }

        #region Phone application initialization

        // Avoid double-initialization
        private bool phoneApplicationInitialized = false;

        // Do not add any additional code to this method
        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            // Create the frame but don't set it as RootVisual yet; this allows the splash
            // screen to remain active until the application is ready to render.
            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated += CompleteInitializePhoneApplication;

            // Handle navigation failures
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            // Ensure we don't initialize again
            phoneApplicationInitialized = true;
        }

        // Do not add any additional code to this method
        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            // Set the root visual to allow the application to render
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;

            // Remove this handler since it is no longer needed
            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }

        #endregion
    }


    /// <summary>
    /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// </summary>
     public class MyDataContext : DataContext  
    {  
        /// <summary>  
        /// 连接字符串  
        /// </summary>  
        public const string ConnectionString = "Data Source='isostore:/MyDb.sdf';Password='123456'";  
        /// <summary>  
        /// 构造函数  
        /// </summary>  
        public MyDataContext() : base(ConnectionString) { }  
  
        public Table<Students> Students;
        public Table<Locations> Locations;
        public Table<ActivityList> ActivityList;

    }

     [Table]
     public class Locations : INotifyPropertyChanged
     {
         string _locationId;
         [Column(CanBeNull = false, IsPrimaryKey = true)]
         public string LocationId
         {
             get
             {
                 return this._locationId;
             }
             set
             {
                 if (_locationId != value)
                 {
                     this._locationId = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("LocationId"));
                 }
             }
         }

         string _locationName;
         [Column(CanBeNull = true)]
         public string LocationName
         {
             get
             {
                 return this._locationName;
             }
             set
             {
                 if (_locationName != value)
                 {
                     this._locationName = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("LocationName"));
                 }
             }
         }

         string _address;
         [Column]
         public string Address
         {
             get
             {
                 return this._address;
             }
             set
             {
                 if (_address != value)
                 {
                     this._address = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("Address"));
                 }
             }
         }

         /// <summary>
         /// //////////////////////////////////////double////////////////////////////
         /// </summary>
         string _coordinate;
         [Column]
         public string Coordinate
         {
             get
             {
                 return this._coordinate;
             }
             set
             {
                 if (_coordinate != value)
                 {
                     this._coordinate = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("Coordinate"));
                 }
             }
         }
         /// <summary>
         /// //////////////////////////////////////double////////////////////////////
         /// </summary>

         public event PropertyChangedEventHandler PropertyChanged;
         protected void OnPropertyChanged(PropertyChangedEventArgs e)
         {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, e);
             }
         }
     }


     [Table]
     public class ActivityList : INotifyPropertyChanged
     {
         string _activityId;
         [Column(CanBeNull = false, IsPrimaryKey = true)]
         public string ActivityId
         {
             get
             {
                 return this._activityId;
             }
             set
             {
                 if (_activityId != value)
                 {

                     this._activityId = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("ActivityId"));
                 }
             }
         }


         string _eventName;
         [Column]
         public string EventName
         {
             get
             {
                 return this._eventName;
             }
             set
             {
                 if (_eventName != value)
                 {

                     this._eventName = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("EventName"));
                 }
             }
         }


         string _note;
         [Column]
         public string Note
         {
             get
             {
                 return this._note;
             }
             set
             {
                 if (_note != value)
                 {

                     this._note = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("Note"));
                 }
             }
         }

         /// <summary>
         /// //////////////////////////Time Attribute
         /// </summary>


         string _locationId;
         [Column]
         public string LocationId
         {
             get
             {
                 return this._locationId;
             }
             set
             {
                 if (_locationId != value)
                 {
                     this._locationId = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("LocationId"));
                 }
             }
         }

         bool _status;
         [Column]
         public bool Statue
         {
             get
             {
                 return this._status;
             }
             set
             {
                 if (_status != value)
                 {
                     this._status = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("LocationId"));
                 }
             }
         } 

         public event PropertyChangedEventHandler PropertyChanged;
         protected void OnPropertyChanged(PropertyChangedEventArgs e)
         {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, e);
             }
         }
     }

     ////////////////////////////////////////////////
     [Table]
     public class Students : INotifyPropertyChanged
     {
         string _stuNo;
         /// <summary>  
         /// 学号  
         /// </summary>  
         [Column(CanBeNull = false, IsPrimaryKey = true)]
         public string StuNo
         {
             get
             {
                 return this._stuNo;
             }
             set
             {
                 if (_stuNo != value)
                 {

                     this._stuNo = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("StuNo"));
                 }
             }
         }

         string _name;
         /// <summary>  
         /// 姓名  
         /// </summary>  
         [Column]
         public string Name
         {
             get
             {
                 return this._name;
             }
             set
             {
                 if (_name != value)
                 {

                     _name = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("Name"));
                 }
             }
         }

         string _email;
         /// <summary>  
         /// 电邮  
         /// </summary>  
         [Column]
         public string Email
         {
             get
             {
                 return this._email;
             }
             set
             {
                 if (_email != value)
                 {

                     _email = value;
                     OnPropertyChanged(new PropertyChangedEventArgs("Email"));
                 }
             }
         }



         public event PropertyChangedEventHandler PropertyChanged;



         protected void OnPropertyChanged(PropertyChangedEventArgs e)
         {
             if (PropertyChanged != null)
             {
                 PropertyChanged(this, e);
             }
         }
     }
  
}